window.addEventListener("load", function load(event) {
  console.log('load');
  reloadImages();
  document.querySelector('#reload').addEventListener('click', reloadImages);
  document.querySelector('#settings').addEventListener('click', function () {
    if (chrome.runtime.openOptionsPage) {
      chrome.runtime.openOptionsPage();
    } else {
      window.open(chrome.runtime.getURL('options.html'));
    }
  });
  chrome.storage.sync.get({
    minHeight: 350,
    minWidth: 350
  }, function (opt) {
    document.querySelector('#settingText').textContent = opt.minWidth + 'x' + opt.minHeight;
  });
}, false);


function reloadImages() {
  sendMessageToContentScript({ type: 'reload' }, function (response) {
    console.log('来自content的回复：' + response);
    renderImages();
  });
}



function renderImages() {
  var bg = chrome.extension.getBackgroundPage();
  if (!bg) {
    bg = [
      'http://s6.sinaimg.cn/mw690/001lytAhzy7n3fUXcBD55&690',
      'http://s16.sinaimg.cn/mw690/001lytAhzy7n3fUM69Vdf&690'
    ];
  }
  if (bg.collectedImages.length < 1) {
    return;
  }

  var lis = [];
  for (var i in bg.collectedImages) {
    var o = bg.collectedImages[i];
    lis.push('<div class="card" v-for="img in images"> ' +
      '<img class="card-img-top" src="' + o + '" alt="Card image cap" >' +
      '<div class="card-body">' +
      '<p class="card-text"><label><input type="checkbox" class="selectedImages" checked>下载此照片</label>  ' +
      '<a href="' + o + '" target="_blank"><i class="fas fa-external-link-alt"></i> 新窗口打开图片</a></p>' +
      '</div></div>');
  }


  document.querySelector('#ext_images_list').innerHTML = lis.join('');
}

